/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.Date;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import pojo.Chitietphieunhaphang;
import pojo.Nhavien;
import pojo.Quanlysuthaydoi;
import util.HibernateUtil;

/**
 *
 * @author FT
 */
public class QuanLySuThayDoiDAO {
    public static List<Quanlysuthaydoi> layDanhSachSuThayDoi() {
        List<Quanlysuthaydoi> ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "select sv from Quanlysuthaydoi sv";
            Query query = session.createQuery(hql);
            ds = query.list();
        } catch (HibernateException ex) {
            //Log the exception
            System.err.println(ex);
        } finally {
            session.close();
        }
        return ds;
    }
    
    public static String MaTangTuDong() {
        String chuoi = "";
        String macuoicung = "";
        List<Quanlysuthaydoi> listpnh = QuanLySuThayDoiDAO.layDanhSachSuThayDoi();
        if (QuanLySuThayDoiDAO.layDanhSachSuThayDoi().size()==0){
            chuoi += "qtd001";
            return chuoi;
        }
        macuoicung += listpnh.get(QuanLySuThayDoiDAO.layDanhSachSuThayDoi().size() - 1).getIdquanlysuthaydoi();
        String index = macuoicung.substring(3);
        try {

            //System.out.println("Index la"+index+"");
            int chiso = Integer.valueOf(index);
            chiso++;

            if (chiso < 10) {
                chuoi += "qtd00" + chiso;
            } else if (chiso >= 10 && chiso < 100) {
                chuoi += "qtd0" + chiso;
            } else if (chiso >= 1000) {
                chuoi += "qtd" + chiso;
            }
        } catch (Exception e) {
            chuoi += "NA";
        }
        return chuoi;
    }
    
    public static void Insert(String mathaydoi, Date ngay, String maphieuthaydoi, int tinhtrang, String nhanvien, String bangthaydoi){
        Session session = HibernateUtil.getSessionFactory().openSession();
        Quanlysuthaydoi ql = new Quanlysuthaydoi(mathaydoi);
        ql.setNgaythaydoi(ngay);
        ql.setMaphieuthaydoi(maphieuthaydoi);
        ql.setTinhtrang(tinhtrang);
        ql.setBangthaydoi(bangthaydoi);
        Nhavien nv = NhanVienDAO.GetNhanVien(nhanvien);
        ql.setNhavien(nv);
         try {
            session.beginTransaction();
           
            session.save(ql);
            session.getTransaction().commit();
            
        } catch (HibernateException ex) {
            //Log the exception
            System.err.println(ex);
        } finally {
            session.close();
            
        }
    }
    
    public static String QuanLy(String bang){
        String chuoi="";
        List<Quanlysuthaydoi> listql = QuanLySuThayDoiDAO.layDanhSachSuThayDoi();
        for (int i=0;i<listql.size();i++){
            if (listql.get(i).getBangthaydoi().equals(bang)){
                Date ngay = listql.get(i).getNgaythaydoi();
                //chuoi+=ngay.getDate()+"-"+ngay.getMonth()+"-"+ngay.getYear()+" "+ngay.getHours()+":"+ngay.getMinutes()+":"+ngay.getMinutes();
                chuoi+=ngay.toString();
                chuoi+=" "+""+listql.get(i).getNhavien().getIdnhavien()+"";
                if(listql.get(i).getTinhtrang()==0){
                    chuoi+=" xoa ";
                }
                if(listql.get(i).getTinhtrang()==1){
                    chuoi+=" them ";
                }
                if(listql.get(i).getTinhtrang()==2){
                    chuoi+=" chinh sua ";
                }
                chuoi+=bang+" ";
                chuoi+=listql.get(i).getMaphieuthaydoi()+" \n";
            }
        }
        return chuoi;
    }
}
